/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.manager;

import lt.marsrutai.mobile.android.activity.LegalNoticesActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;

/**
 *
 * @author Martynas
 *
 */
public class AboutPreferenceManager extends AppPreferenceManager {

    private static final String KEY_LEGAL_NOTICES = "legalNotices";
    private static final String KEY_VERSION = "version";
    private static final String KEY_DEVELOPERS = "developers";
    private static final String KEY_PAGE = "page";
    private static final String KEY_ISSUES = "issues";
    private static final String KEY_EMAIL = "email";

    public AboutPreferenceManager(Activity activity, Object caller) {
        super(activity, caller);
    }

    public void registerListeners() {
        String packageName;
        try {
            packageName = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0).versionName;
        }
        catch (NameNotFoundException e) {
            packageName = "N/A";
        }

        Preference versionPreference = findPreference(KEY_VERSION);
        versionPreference.setSummary(packageName);
        
        Preference developersPreference = findPreference(KEY_DEVELOPERS);
        developersPreference.setOnPreferenceClickListener(new OpenSummaryLinkListener());
        
        Preference pagePreference = findPreference(KEY_PAGE);
        pagePreference.setOnPreferenceClickListener(new OpenSummaryLinkListener());
        
        Preference issuesPreference = findPreference(KEY_ISSUES);
        issuesPreference.setOnPreferenceClickListener(new OpenSummaryLinkListener());
        
        Preference emailPreference = findPreference(KEY_EMAIL);
        emailPreference.setOnPreferenceClickListener(new OpenSummaryLinkListener());

        Preference legalNoticePreference = findPreference(KEY_LEGAL_NOTICES);
        legalNoticePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            
            @Override
            public boolean onPreferenceClick(Preference preference) {
                activity.startActivity(new Intent(activity, LegalNoticesActivity.class));
                return true;
            }
        });
    }
    
    private class OpenSummaryLinkListener implements OnPreferenceClickListener {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            String url = preference.getSummary().toString();
            if (!url.startsWith("http") && url.contains("@")) {
                // Looks like email
                Intent i = new Intent(Intent.ACTION_SEND);
                i.putExtra(Intent.EXTRA_EMAIL, new String[]{url});
                i.setType("plain/text");               
                activity.startActivity(Intent.createChooser(i, "E-Mail")); // Need to do this, otherwise it crashes if no email program available                
            }
            else {
                // Otherwise treat as url
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                activity.startActivity(i);
            }
            return true;
        }
        
    }

}
